export default defineEventHandler(async (event) => {
  // 获取查询参数
  const query = getQuery(event)
  const keyword = query.q || '卫衣'
  
  try {
    // 调用淘宝建议接口
    const url = `https://suggest.taobao.com/sug?code=utf-8&q=${encodeURIComponent(keyword as string)}&callback=cb`
    
    const response = await fetch(url, {
      headers: {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
      }
    })
    
    const text = await response.text()
    
    // 解析 JSONP 响应: cb({"result":[...]})
    const jsonMatch = text.match(/cb\((.*)\)/)
    if (jsonMatch && jsonMatch[1]) {
      const data = JSON.parse(jsonMatch[1])
      return data
    }
    
    return { result: [] }
  } catch (error) {
    console.error('淘宝接口调用失败:', error)
    return { result: [], error: '接口调用失败' }
  }
})
